<template>
  <Modal
    id="sysSystemFormDialog"
    v-model="initOption.showModal"
    :mask-closable="false">
    <p slot="header" style="color:#f60;text-align:center;">
      <Icon type="information-circled"></Icon>
      <span>{{initOption.title}}</span>
    </p>
    <Form
      ref="sysForm"
      :model="sysForm"
      :rules="formRules"
      :label-width="90">
      <Form-item prop="id" v-if="initOption.action === 'edit'">
        <input type="hidden" :model="sysForm.id">
      </Form-item>
      <Form-item label="系统名称" prop="name">
        <Input type="text" v-model="sysForm.name" placeholder="请输入系统名称"></Input>
      </Form-item>
      <Form-item label="描述" prop="description">
        <Input
          type="textarea"
          v-model="sysForm.description"
          placeholder="请输入描述"></Input>
      </Form-item>
    </Form>
    <div slot="footer">
      <Button type="primary" @click="handleSubmit">保存</Button>
      <Button type="error" @click="reset" v-if="initOption.action === 'add'">重置</Button>
    </div>
  </Modal>
</template>
<script>
import {saveSystemInfo, updateSystemInfo, getSystemInfo} from '@/apis/admin/system';

export default {
  name: 'sysSystemFormDialog',
  props: {
    initOption: {
      title: {
        type: String,
        default: '新增系统'
      },
      action: {
        type: String,
        default: 'add'
      },
      showModal: {
        type: Boolean
      },
      id: {
        type: String
      }
    }
  },
  data () {
    return {
      formRules: {
        name: [
          {required: true, message: '系统名称不能为空', trigger: 'blur'}
        ]
      },
      sysForm: {
        id: this.initOption.id,
        name: '',
        description: ''
      }
    };
  },
  methods: {
    getInfo () {
      getSystemInfo(this.initOption.id).then(response => {
        if (response.data.success) {
          this.sysForm = response.data.obj;
        } else {
          this.$Modal.error({
            title: '提示',
            content: response.data.msg
          });
        }
      });
    },
    handleSubmit () {
      this.$refs.sysForm.validate((valid) => {
        if (valid) {
          if (this.initOption.action === 'add') {
            saveSystemInfo(this.sysForm).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.initOption.action === 'edit') {
            updateSystemInfo(this.sysForm).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult (obj) {
      if (obj.success) {
        this.$Notice.success({
          title: '提示',
          desc: obj.msg
        });
        this.$parent.$children[0].query();
        this.$parent.sysFormInitOption.showModal = false;
      } else {
        this.$Notice.error({
          title: '提示',
          desc: obj.msg
        });
      }
    },
    reset () {
      this.$refs.sysForm.resetFields();
    }
  }
};
</script>
<style>
</style>
